using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using DbDiff.DbDiffWinClient.Helper;

namespace DbDiff.DbDiffWinClient.Forms
{
	/// <summary>
	/// Summary description for frmStatus.
	/// </summary>
	public class frmStatus : System.Windows.Forms.Form
	{
		private const int PANELMINHEIGHT = 141;
		private const int PANELMAXHEIGHT = 300;
		private const int GROUPMINHEIGHT = 160;
		private const int LABELHEIGHT = 16;
		private const int CLOSEBUTTONTOP = 216;
		private const string CONTROLNAMEPREFIX = "lbl";
		private const int LEFTPOSITIONDEFAULT = 20;
		private bool _mustClose;
		private EventStatusInfoCollection _eventStatusInfos;

		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.GroupBox grpMain;
		private System.Windows.Forms.Panel panelMain;
		private System.Windows.Forms.GroupBox grpTitle;
		private System.Windows.Forms.Label lblTitle;
	
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmStatus()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			_eventStatusInfos = new EventStatusInfoCollection();
			_mustClose = true;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnClose = new System.Windows.Forms.Button();
			this.grpMain = new System.Windows.Forms.GroupBox();
			this.panelMain = new System.Windows.Forms.Panel();
			this.grpTitle = new System.Windows.Forms.GroupBox();
			this.lblTitle = new System.Windows.Forms.Label();
			this.grpMain.SuspendLayout();
			this.grpTitle.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnClose
			// 
			this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnClose.Location = new System.Drawing.Point(176, 216);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new System.Drawing.Size(80, 23);
			this.btnClose.TabIndex = 0;
			this.btnClose.Text = "Close";
			// 
			// grpMain
			// 
			this.grpMain.Controls.Add(this.panelMain);
			this.grpMain.Location = new System.Drawing.Point(8, 48);
			this.grpMain.Name = "grpMain";
			this.grpMain.Size = new System.Drawing.Size(416, 160);
			this.grpMain.TabIndex = 2;
			this.grpMain.TabStop = false;
			// 
			// panelMain
			// 
			this.panelMain.AutoScroll = true;
			this.panelMain.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panelMain.Location = new System.Drawing.Point(3, 16);
			this.panelMain.Name = "panelMain";
			this.panelMain.Size = new System.Drawing.Size(410, 141);
			this.panelMain.TabIndex = 0;
			// 
			// grpTitle
			// 
			this.grpTitle.Controls.Add(this.lblTitle);
			this.grpTitle.Location = new System.Drawing.Point(8, 0);
			this.grpTitle.Name = "grpTitle";
			this.grpTitle.Size = new System.Drawing.Size(416, 48);
			this.grpTitle.TabIndex = 3;
			this.grpTitle.TabStop = false;
			// 
			// lblTitle
			// 
			this.lblTitle.Location = new System.Drawing.Point(8, 16);
			this.lblTitle.Name = "lblTitle";
			this.lblTitle.Size = new System.Drawing.Size(400, 24);
			this.lblTitle.TabIndex = 0;
			this.lblTitle.Text = "TITLE";
			this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// frmStatus
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnClose;
			this.ClientSize = new System.Drawing.Size(430, 246);
			this.ControlBox = false;
			this.Controls.Add(this.grpMain);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.grpTitle);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MinimumSize = new System.Drawing.Size(432, 248);
			this.Name = "frmStatus";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Load += new System.EventHandler(this.frmStatus_Load);
			this.grpMain.ResumeLayout(false);
			this.grpTitle.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		

		public void Init(string title, EventStatusInfoCollection statusInfos) 
		{
			lblTitle.Text = title;
			_eventStatusInfos = statusInfos;
			_mustClose = true;

			int panelHeight = _eventStatusInfos.Count * LABELHEIGHT;
			if (panelHeight > PANELMINHEIGHT) 
			{
				if (panelHeight > PANELMAXHEIGHT)
					panelHeight = PANELMAXHEIGHT;

				int diff = panelHeight - PANELMINHEIGHT;
				this.Height = this.MinimumSize.Height + diff;
				grpMain.Height = GROUPMINHEIGHT + diff;
				panelMain.Height = PANELMINHEIGHT + diff;
				btnClose.Top = CLOSEBUTTONTOP + diff;
			}
		}
		public void Init(string title, EventStatusInfoCollection statusInfos, bool mustClose) 
		{
			int diff = 0;
			lblTitle.Text = title;
			_eventStatusInfos = statusInfos;
			_mustClose = mustClose;

			int panelHeight = _eventStatusInfos.Count * LABELHEIGHT;
			if (panelHeight > PANELMINHEIGHT) 
			{
				if (panelHeight > PANELMAXHEIGHT)
					panelHeight = PANELMAXHEIGHT;

				diff = panelHeight - PANELMINHEIGHT;
				this.Height = this.MinimumSize.Height + diff;
				grpMain.Height = GROUPMINHEIGHT + diff;
				panelMain.Height = PANELMINHEIGHT + diff;
				btnClose.Top = CLOSEBUTTONTOP + diff;
			}

			btnClose.Visible = _mustClose;
			
			if (! _mustClose)
			{
				diff = btnClose.Top + btnClose.Height - (grpMain.Top + grpMain.Height);
				grpMain.Height += diff;
				panelMain.Height += diff;
			}
		}
		
		private void UpdateView() 
		{
			panelMain.Controls.Clear();
			Label siLabel;
			int top, left;

			left = LEFTPOSITIONDEFAULT;
			top = 0;
			
			EventStatusInfo si;
			foreach (string key in _eventStatusInfos.KeysOriginalOrder) 
			{
				si = _eventStatusInfos[key];

				siLabel = new Label();
				siLabel.Name = CONTROLNAMEPREFIX + si.Id;
				if (si.Description.Length == 0) 
					siLabel.Text = si.Id;
				else
					siLabel.Text = si.Description;
				siLabel.Top = top;
				siLabel.Left = left;
				siLabel.Height = LABELHEIGHT;
				siLabel.TextAlign = ContentAlignment.MiddleLeft;
				siLabel.Width = panelMain.Width - 2*left;
				siLabel.Visible = true;

				panelMain.Controls.Add(siLabel);

				top += siLabel.Height;
			}
		}

		public void HideForm() 
		{
			if (! _mustClose) 
				this.Hide();
		}

		private void frmStatus_Load(object sender, System.EventArgs e)
		{
			UpdateView();
		}
		private void UpdateItem(string eventStatusId) 
		{
			string controlName = CONTROLNAMEPREFIX + eventStatusId;
			string text;

			foreach (Control control in panelMain.Controls) 
			{
				if ((control is Label) &&
					(control.Name == controlName)) 
				{
					text = (_eventStatusInfos[eventStatusId].Description.Length > 0 ? _eventStatusInfos[eventStatusId].Description : _eventStatusInfos[eventStatusId].Id);
					control.Text = _eventStatusInfos[eventStatusId].State + " " + text;
					break;
				}
			}
		}
		/// <summary>
		/// Sets the state of the event status.
		/// </summary>
		/// <param name="eventStatusId">Event status id.</param>
		/// <param name="state">State.</param>
		/// <param name="result">Result.</param>
		public void SetEventStatusState(string eventStatusId, EventState state, EventResult result) 
		{
			if (_eventStatusInfos.ContainsKey(eventStatusId)) 
			{
				_eventStatusInfos[eventStatusId].State = state;
				_eventStatusInfos[eventStatusId].Result = result;
			}
			UpdateItem(eventStatusId);
		}
	}
}